#ifndef _CENTER_HAPPYFARM_MAMANAGER_H_
#define _CENTER_HAPPYFARM_MAMANAGER_H_

#include "Common.h"
#include "Resource.h"
#include "ResourceManager.h"
#include "Singleton.h"
#include "String.h"
#include "Database/Field.h"
#include "DataManager.h"
#include "Threading/Mutex.h"

#define DB_NAME_CENTERHAPPYFARM		"center_happy_farm"

class  Database;
class  UserPtr;
class  CharPtr;
class  ItemPtr;
class  WorldPacket;
class  WSSocketPtr;

typedef struct center_happyfarm_tag_
{
	uint32 farm_id;			// 牧场ID
	uint8 status;			// 宠物状态
	uint32 purchaser_pid;	// 购买者PID
	uint32 owner_pid;		// 当前拥有者PID
	String owner_name;		// 购买者昵称
	String owner_head;		// 购买者头像
	
	uint32 model_id;		// 宠物模式ID
	String name;			// 宠物名称
	uint32 price;			// 价格
	uint32 ripe_coins;		// 成熟后获得的铜钱
	uint32 born_time;		// 出生时间
	uint32 ripe_time;		// 成熟时间
	
	String create_time;		// 创建时间
	String end_time;		// 结束时间
	
	center_happyfarm_tag_()
	{
		Init();
	}
	
	void Init()
	{
		farm_id = 0;			// 牧场ID
		status = 0;				// 宠物状态
		purchaser_pid = 0;		// 购买者PID
		owner_pid = 0;			// 当前拥有者PID
		owner_name = "";		// 购买者昵称
		owner_head = "";		// 购买者头像
		
		model_id = 0;			// 宠物模式ID
		name = "";				// 宠物名称
		price = 0;				// 价格
		ripe_coins = 0;			// 成熟后获得的铜钱
		born_time = 0;			// 出生时间
		ripe_time = 0;			// 成熟时间
		
		create_time = "";		// 创建时间
		end_time = "";			// 结束时间
	}
	
}CenterHappyFarm;

struct stFarmNameAndNum
{
	uint32 num;		//数量
	uint32 price;	//总价
	
	String name;	//名字
	
	stFarmNameAndNum()
	{
		num = 0;
		price = 0;
		
		name = "";
	}
};

enum enHappyFarmStatus
{
	enHappyFarmStatus_Default = 0,		// 默认
	enHappyFarmStatus_Alive = 1,		// 存活
	enHappyFarmStatus_Delete = 255,		// 删除
};

enum enHappyFarmLogSubType
{
	enHappyFarmLogSubType_Buy = 1,		// 买
	enHappyFarmLogSubType_Sell = 2,		// 卖
	enHappyFarmLogSubType_Give = 3,		// 赠送
	enHappyFarmLogSubType_Recv = 4,		// 获赠
	enHappyFarmLogSubType_GwAdd = 100,	// 网关添加
};

enum enSendResult		// 宠物赠送结果
{
	enSendResult_Sucess = 0,		// 成功
	enSendResult_ErrorPid = 1,		// 无效的PID
	enSendResult_OverLimit = 2,		// 超过目标玩家上限
};

#define WGS_CENTERHAPPYFARM_MUTEX_NAME centerhappyfarm_mutex
#define WGS_CENTERHAPPYFARM_MUTEX mutable boost::mutex WGS_CENTERHAPPYFARM_MUTEX_NAME;
#define WGS_CENTERHAPPYFARM_LOCK		boost::lock_guard<boost::mutex> wgsCenterHappyFarmLock(WGS_CENTERHAPPYFARM_MUTEX_NAME);

//-------------------------组局信息-----------------------
class  SERVER_DECL CenterHappyFarmManager:public Singleton<CenterHappyFarmManager>
{
public:
	WGS_CENTERHAPPYFARM_MUTEX
	/**
	 * @创建时间 2016-07-13
	 * @创建人 Roach
	 * @函数作用 添加一条CenterHappyFarm记录
	 * @参数
	 * 	@cb	CenterHappyFarm
	 */
	bool AddCenterHappyFarm(CenterHappyFarm & cb);
	
	/**
	 * @创建时间 2016-07-13
	 * @创建人 Roach
	 * @函数作用 更新friendrecord数据
	 * @参数
	 * 	@cb CenterHappyFarm
	 */
	bool UpdateCenterHappyFarm(const CenterHappyFarm & cbh);
	
	/**
	 * @创建时间 2016-07-13
	 * @创建人 Roach
	 * @函数作用 更新friendrecord数据
	 * @参数
	 * 	@cfr_id	
	 *	@fields	字段键值对,格式：fields["key1"] = 1; fields["key2"] = 2;...
	 */
	bool UpdateCenterHappyFarm(Fields * fields, const char * QueryString, ...);
	bool UpdateCenterHappyFarm(const uint32 &farm_id, Fields * fields);
	
	/**
	 * @创建时间 2016-07-13
	 * @创建人 Roach
	 * @函数作用 根据PD_ID获取一条PD数据
	 * @参数
	 *  @cfr_id CenterHappyFarm id
	 * 	@cb		CenterGameFriend对象
	 */
	bool GetCenterHappyFarm(const uint32 &farm_id, CenterHappyFarm * cb);
	
	/**
	 * @创建时间 2016-07-13
	 * @创建人 Roach
	 * @函数作用 获取记录列表
	 * @参数
	 * 	@lstData	CenterFriendRecord对象列表
	 *  @query 查询语句条件
	 */
	uint32 GetCenterHappyFarmList(std::list<CenterHappyFarm> * lstData, const char * query,...);
	
public:
	// 购买动物
	bool BuyAnimal(UserPtr & pUser, const uint32 & model_id, const uint32 & num);
	bool AddAnimal(UserPtr & pUser, const uint32 & model_id, const uint32 & num);
	
	// 出售动物
	bool SellAnimal(UserPtr & pUser, const uint32 & farm_id);
	// 出售所有动物
	void SellAllAnimals(UserPtr & pUser);
	// 赠送动物
	bool TransAnimal(UserPtr & pUser, const uint32 & dest_pid, const uint32 & farm_id);
	// 获得当前玩家宠物库存
	String GetPlayerAnimalsMap(const uint32 & pid);
	// 获得玩家宠物列表
	void GetHappyFarmList(UserPtr & pUser);
	
	//================================日志相关==================================
	// 宠物购买日志
	bool HappyFarmBuySellLog(UserPtr &pUser, 
							const uint8 &opType,
							const uint32 &model_id,
							const String &szName, 
							const uint32 &num, 
							const uint32 &coins, 
							String strKucun);
	
	// 宠物赠送日志
	bool HappyFarmGiveLog(UserPtr &pUser, CenterHappyFarm &chf, String strKucun);
	// 宠物获赠日志
	bool HappyFarmRecvLog(const uint32 &recv_pid, String &recv_name, UserPtr &pGiveUser, CenterHappyFarm &chf, String strKucun);
	// 将json格式的牧场数据转换为牧场数据库存储
	void TranJsonDataToNewHappyFarm(UserPtr & pUser);
	
public:
	//=========================协议相关============================
	// 创建我的牧场数据包
	bool CreateMyHappyFarmPacket(WorldPacket *packet, std::list<CenterHappyFarm> &pList);
	// 创建买卖动物数据包
	bool CreateBusinessHappyFarmPacket(WorldPacket *packet, uint8 result, uint8 type, const uint32 &farmId, const String &strName, uint16 number, uint32 getCoins);
	// 创建赠送动物数据包
	bool CreateGivingHappyFarmPacket(WorldPacket *packet, uint8 result, uint32 farmId, uint32 destPId, String strNick);
	// 创建赠送记录数据包
	bool CreateGivingRecodePacket(WorldPacket *packet, uint32 platformId);
	// 创建可购买动物列表
	bool CreateBuyFarmListPacket(WorldPacket *packet);
	// 创建单动物更新包
	bool CreateSingleFarmPacket(WorldPacket *packet, const CenterHappyFarm &chf);
	
};

#define	 sCenterHappyFarmMgr		 CenterHappyFarmManager::getSingleton()


#endif
